package com.lxw.station.chatgpt;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class ChatGptFormSimulation {

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();

        // 登录后的会话Cookie，从浏览器中获取
        String sessionCookie = "SESSION=your_session_cookie"; // 替换为实际的会话Cookie

        // ChatGPT互动的真实URL
        String chatGptUrl = "https://chat.openai.com/backend-api/conversation"; // 从浏览器开发者工具中获取实际URL

        // 设置请求头，包括会话Cookie
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json");
        headers.set("Cookie", sessionCookie);
        headers.set("Authorization", "Bearer your_access_token"); // 替换为实际的Bearer令牌

        // 设置请求体
        String requestBody = "{"
                + "\"model\": \"gpt-3.5-turbo\","
                + "\"messages\": [{\"role\": \"user\", \"content\": \"Hello, how are you?\"}]"
                + "}";

        // 将请求头和请求体封装成HttpEntity对象
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);

        // 发送POST请求并获取响应
        ResponseEntity<String> response = restTemplate.exchange(chatGptUrl, HttpMethod.POST, requestEntity, String.class);

        // 输出响应结果
        System.out.println(response.getBody());
    }
}

